//Pins used
const int knobpin = A0;
const int ledpin = 13;
const int chargepin = 12;
const int electrodepin = 11;
const int switchpin = 10;

void setup()
{
  pinMode(switchpin, INPUT);
  pinMode(chargepin, OUTPUT);
  pinMode(electrodepin, OUTPUT);
  Serial.begin(9600);
}

void loop()
{
  digitalWrite(chargepin, digitalRead(switchpin)); //charge capacitor iff switch is on
  
  if(digitalRead(switchpin) == HIGH) //if switch is on
  {
    //Read knob connected to A0 and figure out frequency
    unsigned int analogVal = analogRead(knobpin);
    unsigned long interval = 1 / map(analogVal, 0, 1023, 0.5, 10);
    //Send a square pulse of the desired frequency (electrodes and LED)
    delay(interval);
    digitalWrite(electrodepin, HIGH);
    digitalWrite(ledpin, HIGH);
    delayMicroseconds(100);
    digitalWrite(electrodepin, LOW);
    digitalWrite(ledpin, LOW);
    if(millis() % 30000 == 0)        //Every 30 seconds, print to serial
    {
      Serial.print("Electroporating with frequency ");
      Serial.print(1 / interval);
      Serial.println(" Hz.");
    }
  }
}
